package name.jiaosha.air.iostorage.controller;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import name.jiaosha.air.AppConstants;
import name.jiaosha.air.company.domain.Part;
import name.jiaosha.air.company.domain.PartPackage;
import name.jiaosha.air.iostorage.domain.Buy;
import name.jiaosha.air.iostorage.domain.BuyItem;
import name.jiaosha.air.iostorage.service.IStorageService;

import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

/**
 * 入库控制器
 * 
 * @author xu
 * 
 */
@Controller
@RequestMapping("/admin/storage/")
public class InputStorageController {

	@Autowired
	private IStorageService storageService;

	@RequestMapping("/getBuyItems")
	public ModelAndView getBuyItems() {

		List<BuyItem> list = null;
		int total = 0;

		if (AppConstants.DEBUG) {
			list = new ArrayList<BuyItem>();
			for (int i = 0; i < 10; i++) {
				BuyItem buyItem = new BuyItem();
				buyItem.setId(i);
				buyItem.setDocID("20100910001");
				buyItem.setName("底盘部件W240L.1" + i);
				buyItem.setProductCode("201245240359" + i);
				buyItem.setAmount(new BigDecimal(10 + i));
				buyItem.setUnit("个");
				buyItem.setUnitPrice(new BigDecimal(50 + i));
				buyItem.setStorageId(i);
				list.add(buyItem);
			}
			total = list.size();

		} else {

		}

		ModelMap model = new ModelMap();
		model.addAttribute("root", list);
		model.addAttribute("totalProperty", total);
		return new ModelAndView("jsonView", model);
	}

	@RequestMapping("/createBuy")
	public ModelAndView createPart(@RequestBody String jsonData) {

		if (!AppConstants.DEBUG) {

		} else {
			ObjectMapper mapper = new ObjectMapper();
			Map<String, Object> data;
			try {
				data = mapper.readValue(jsonData, Map.class);

				byte[] b = mapper.writeValueAsBytes(data.get("buy"));
				Buy buy = mapper.readValue(b, 0, b.length, Buy.class);

				buy.setCreateMan("徐声耀");

				b = mapper.writeValueAsBytes(data.get("buyItems"));
				List<BuyItem> buyItems = mapper.readValue(b, 0, b.length,
						new TypeReference<ArrayList<BuyItem>>() {
						});
				buy.setItems(buyItems);
				for (BuyItem item : buy.getItems()) {

					System.out.println(item);

				}
				storageService.createBuy(buy);

			} catch (JsonParseException e) {

				e.printStackTrace();
			} catch (JsonMappingException e) {

				e.printStackTrace();
			} catch (IOException e) {

				e.printStackTrace();
			}

		}

		ModelMap model = new ModelMap();
		model.addAttribute("success", true);
		return new ModelAndView("jsonView", model);
	}

	@RequestMapping("/buyInfo")
	public ModelAndView getBuyInfo(@RequestParam String id) {

		ModelMap model = new ModelMap();
		if (!AppConstants.DEBUG) {

		} else {

			Buy buy = storageService.getBuyById(id);
			if (buy != null) {

				List<BuyItem> list = storageService.getBuyItems(buy.getDocID());
				buy.setItems(list);
			}
			model.put("data", buy);
		}
		model.put("success", "true");
		return new ModelAndView("jsonView", model);
	}

}
